using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spread.Common.Features.Sample;
using FarPoint.Win.Spread;
using Spread.Common.DataStore;

namespace Spread.CellTypes.Features
{
    public partial class AutoCompleteDemo : BaseSampleControl
    {
        private AutoCompleteStringCollection _autoCompleteStringcollection;
        private FarPoint.Win.Spread.CellType.GeneralCellType _autoFillCellType;
        private FarPoint.Win.Spread.CellType.TextCellType _textCellType;
        private int _previousColumn = -1;
        public AutoCompleteDemo()
        {
            InitializeComponent();
            Name = "AutoComplete";
            InitializeControls();
            SetupSpreadSheet();
        }
        private void InitializeControls()
        {
            dataSourceComboBox.DataSource = new string[] { "CustomSource and All Column Items Below Cell", "CustomSource Only", "Column Items Below Cell Only" };
            dataSourceComboBox.SelectedIndex = 0;

            modeComboBox.DataSource = Enum.GetValues(typeof(AutoCompleteMode));
            modeComboBox.SelectedIndex = 3;
        }
        
        private void SetupSpreadSheet()
        {
            spread.Sheets[0].Columns.Default.Width = 90;
            spread.Sheets[0].Rows.Default.Height = 25;
            spread.DataSource = new DataService().AccessDataSource.GetNWINDData("Customers");

            string columnName=null;
            foreach(Column column in sheetView.Columns)
            {
              columnName = string.Format("{0}{1} ({2}) |", columnName, column.Label, column.Index);
            }
            columnName = columnName.TrimEnd('|');
            applyColumnComboBox.DataSource = columnName.Split('|');
            applyColumnComboBox.SelectedIndex = 0;

            string[] suggestionList = "Benjamin,Bruce,Cheryl,Daniel,Dave,Eric,Hana,Harol,Henry,Jane,Jessica,John,Julia,Kathy,Lance,Laura,Mark,Marry,Meggie,Mike,Naomi,Paul,Peter,Roger,Rose,Ruth,Ryan,Shirley, Tan,Thomas,Tony, Varun, $1234, VND1234, 12345 %,13 %, 02/19/1991,Sunday, August 08, 2007,Saturday, March 09, 1997, February,0901234567,0953336650".Split(',');
            customListBox.DataSource = suggestionList;

            _autoCompleteStringcollection = new AutoCompleteStringCollection();
            _autoCompleteStringcollection.AddRange(suggestionList); 
        }
        private void CustomAutoComplete(int column,int acm,int AutoFillAutoCompleteCustomSource)
        {
            _autoFillCellType = new FarPoint.Win.Spread.CellType.GeneralCellType();
            _autoFillCellType.AutoCompleteSource = AutoCompleteSource.CustomSource;

            _textCellType = new FarPoint.Win.Spread.CellType.TextCellType();

            if (AutoFillAutoCompleteCustomSource == 2)
                _autoFillCellType.AutoCompleteCustomSource = null;
            else
                _autoFillCellType.AutoCompleteCustomSource = _autoCompleteStringcollection;
            
            _autoFillCellType.AutoFillAutoCompleteCustomSource = AutoFillAutoCompleteCustomSource!=1;
           
            _autoFillCellType.AutoCompleteMode = (AutoCompleteMode)acm;
            spread.ActiveSheet.Columns[column].CellType = _autoFillCellType;

            if (_previousColumn >= 0 && _previousColumn!=column)
                spread.ActiveSheet.Columns[_previousColumn].CellType = _textCellType;
            
            _previousColumn = column;
        }
        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            CustomAutoComplete(applyColumnComboBox.SelectedIndex, modeComboBox.SelectedIndex, dataSourceComboBox.SelectedIndex);
        }
    }
}


//
//AutoCompleteDemo.Designer.cs
//


using FarPoint.Win.Spread;
namespace Spread.CellTypes.Features
{
    partial class AutoCompleteDemo
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.spread = new FarPoint.Win.Spread.FpSpread();
            this.sheetView = new FarPoint.Win.Spread.SheetView();
            this.autoCompleteGRBox = new System.Windows.Forms.GroupBox();
            this.autoCompleteModeLabel = new System.Windows.Forms.Label();
            this.applyColumnComboBox = new System.Windows.Forms.ComboBox();
            this.customSourceLabel = new System.Windows.Forms.Label();
            this.dataSourceComboBox = new System.Windows.Forms.ComboBox();
            this.applyToColumnLabel = new System.Windows.Forms.Label();
            this.modeComboBox = new System.Windows.Forms.ComboBox();
            this.autoFillDataSourceLabel = new System.Windows.Forms.Label();
            this.customListBox = new System.Windows.Forms.ListBox();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.spread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.sheetView)).BeginInit();
            this.autoCompleteGRBox.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.spread);
            this.pnlMain.Size = new System.Drawing.Size(626, 823);
            // 
            // pnlControls
            // 
            this.pnlControls.AutoScroll = true;
            this.pnlControls.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.pnlControls.Controls.Add(this.autoCompleteGRBox);
            this.pnlControls.Size = new System.Drawing.Size(207, 823);
            // 
            // spread
            // 
            this.spread.AccessibleDescription = "";
            this.spread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.spread.Location = new System.Drawing.Point(0, 0);
            this.spread.Name = "spread";
            this.spread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.sheetView});
            this.spread.Size = new System.Drawing.Size(626, 823);
            this.spread.TabIndex = 0;
            // 
            // sheetView
            // 
            this.sheetView.Reset();
            this.sheetView.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.sheetView.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.sheetView.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // autoCompleteGRBox
            // 
            this.autoCompleteGRBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.autoCompleteGRBox.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.autoCompleteGRBox.Controls.Add(this.autoCompleteModeLabel);
            this.autoCompleteGRBox.Controls.Add(this.applyColumnComboBox);
            this.autoCompleteGRBox.Controls.Add(this.customSourceLabel);
            this.autoCompleteGRBox.Controls.Add(this.dataSourceComboBox);
            this.autoCompleteGRBox.Controls.Add(this.applyToColumnLabel);
            this.autoCompleteGRBox.Controls.Add(this.modeComboBox);
            this.autoCompleteGRBox.Controls.Add(this.autoFillDataSourceLabel);
            this.autoCompleteGRBox.Controls.Add(this.customListBox);
            this.autoCompleteGRBox.Location = new System.Drawing.Point(5, 23);
            this.autoCompleteGRBox.Name = "autoCompleteGRBox";
            this.autoCompleteGRBox.Size = new System.Drawing.Size(150, 309);
            this.autoCompleteGRBox.TabIndex = 0;
            this.autoCompleteGRBox.TabStop = false;
            this.autoCompleteGRBox.Text = "AutoComplete";
            // 
            // autoCompleteModeLabel
            // 
            this.autoCompleteModeLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.autoCompleteModeLabel.AutoSize = true;
            this.autoCompleteModeLabel.Location = new System.Drawing.Point(6, 37);
            this.autoCompleteModeLabel.Name = "autoCompleteModeLabel";
            this.autoCompleteModeLabel.Size = new System.Drawing.Size(106, 13);
            this.autoCompleteModeLabel.TabIndex = 34;
            this.autoCompleteModeLabel.Text = "AutoComplete Mode:";
            // 
            // applyColumnComboBox
            // 
            this.applyColumnComboBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.applyColumnComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.applyColumnComboBox.FormattingEnabled = true;
            this.applyColumnComboBox.Location = new System.Drawing.Point(9, 53);
            this.applyColumnComboBox.Name = "applyColumnComboBox";
            this.applyColumnComboBox.Size = new System.Drawing.Size(121, 21);
            this.applyColumnComboBox.TabIndex = 37;
            this.applyColumnComboBox.SelectedIndexChanged += new System.EventHandler(this.ComboBox_SelectedIndexChanged);
            // 
            // customSourceLabel
            // 
            this.customSourceLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.customSourceLabel.AutoSize = true;
            this.customSourceLabel.Location = new System.Drawing.Point(6, 86);
            this.customSourceLabel.Name = "customSourceLabel";
            this.customSourceLabel.Size = new System.Drawing.Size(110, 13);
            this.customSourceLabel.TabIndex = 38;
            this.customSourceLabel.Text = "Custom Source Items:";
            // 
            // dataSourceComboBox
            // 
            this.dataSourceComboBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.dataSourceComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.dataSourceComboBox.FormattingEnabled = true;
            this.dataSourceComboBox.Location = new System.Drawing.Point(9, 102);
            this.dataSourceComboBox.Name = "dataSourceComboBox";
            this.dataSourceComboBox.Size = new System.Drawing.Size(121, 21);
            this.dataSourceComboBox.TabIndex = 33;
            this.dataSourceComboBox.SelectedIndexChanged += new System.EventHandler(this.ComboBox_SelectedIndexChanged);
            // 
            // applyToColumnLabel
            // 
            this.applyToColumnLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.applyToColumnLabel.AutoSize = true;
            this.applyToColumnLabel.Location = new System.Drawing.Point(6, 130);
            this.applyToColumnLabel.Name = "applyToColumnLabel";
            this.applyToColumnLabel.Size = new System.Drawing.Size(87, 13);
            this.applyToColumnLabel.TabIndex = 36;
            this.applyToColumnLabel.Text = "Apply To Column";
            // 
            // modeComboBox
            // 
            this.modeComboBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.modeComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.modeComboBox.FormattingEnabled = true;
            this.modeComboBox.Location = new System.Drawing.Point(9, 146);
            this.modeComboBox.Name = "modeComboBox";
            this.modeComboBox.Size = new System.Drawing.Size(121, 21);
            this.modeComboBox.TabIndex = 35;
            this.modeComboBox.SelectedIndexChanged += new System.EventHandler(this.ComboBox_SelectedIndexChanged);
            // 
            // autoFillDataSourceLabel
            // 
            this.autoFillDataSourceLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.autoFillDataSourceLabel.AutoSize = true;
            this.autoFillDataSourceLabel.Location = new System.Drawing.Point(6, 182);
            this.autoFillDataSourceLabel.Name = "autoFillDataSourceLabel";
            this.autoFillDataSourceLabel.Size = new System.Drawing.Size(104, 13);
            this.autoFillDataSourceLabel.TabIndex = 32;
            this.autoFillDataSourceLabel.Text = "AutoFill Data Source";
            // 
            // customListBox
            // 
            this.customListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.customListBox.FormattingEnabled = true;
            this.customListBox.Location = new System.Drawing.Point(9, 198);
            this.customListBox.Name = "customListBox";
            this.customListBox.Size = new System.Drawing.Size(121, 95);
            this.customListBox.TabIndex = 39;
            // 
            // AutoCompleteDemo
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "AutoCompleteDemo";
            this.Size = new System.Drawing.Size(834, 823);
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.spread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.sheetView)).EndInit();
            this.autoCompleteGRBox.ResumeLayout(false);
            this.autoCompleteGRBox.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion
        private FpSpread spread;
        private SheetView sheetView;
        private System.Windows.Forms.GroupBox autoCompleteGRBox;
        private System.Windows.Forms.Label autoCompleteModeLabel;
        private System.Windows.Forms.ComboBox applyColumnComboBox;
        private System.Windows.Forms.Label customSourceLabel;
        private System.Windows.Forms.ComboBox dataSourceComboBox;
        private System.Windows.Forms.Label applyToColumnLabel;
        private System.Windows.Forms.ComboBox modeComboBox;
        private System.Windows.Forms.Label autoFillDataSourceLabel;
        private System.Windows.Forms.ListBox customListBox;
    }
}
